package com.dawnClass.controller;

import com.dawnClass.pojo.Consumer;
import com.dawnClass.service.InformationService;
import com.dawnClass.util.ServiceResponse;
import com.dawnClass.util.UploadImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class InformationController {
    @Autowired
    private InformationService informationService;



    //加载个人课程数据,个人信息
    @GetMapping("consumerInformation")
    @ResponseBody
    public ModelAndView  index(HttpSession session){
        //获取session中存储的consumer
        ServiceResponse consumerResponse = (ServiceResponse)session.getAttribute("ServiceResponse");
        Consumer consumer = (Consumer) consumerResponse.getdata();

        //通过用户id查询用户课程
        ServiceResponse consumerInformation = informationService.getByConsumerId(consumer.getConsumerId());

        System.out.println("consumerInformation"+consumerInformation);
        ModelAndView mav = new ModelAndView();

        mav.addObject("consumerInformation",consumerInformation);
        mav.setViewName("user/information");

        return mav;
    }




    @GetMapping("information")
    public ModelAndView getById (HttpSession session){
        //获取session中存储的consumer
        ServiceResponse consumerResponse = (ServiceResponse)session.getAttribute("ServiceResponse");
        Consumer consumer = (Consumer) consumerResponse.getdata();
        ModelAndView mav = new ModelAndView();
        ServiceResponse serviceResponse3 = informationService.getById(consumer.getConsumerId());
        System.out.println(serviceResponse3);
        mav.addObject("serviceResponse3",serviceResponse3);

        mav.setViewName("user/information");
        return mav;
    }




    @PostMapping("consumerUpdate")  // update
/*    @ResponseBody*/
    public ModelAndView update(@RequestParam String consumerName,
                               @RequestParam String consumerGender,
                               @RequestParam String consumerEmail,
                               @RequestParam String consumerHobby,
                               @RequestParam String Other1,
                               @RequestParam String mainImgUrl,
                                HttpSession session){
        //获取用户
        System.out.println(consumerName);
        System.out.println(consumerGender);
        System.out.println(consumerEmail);
        System.out.println(consumerHobby);
        System.out.println(Other1);
        System.out.println(mainImgUrl);
        ServiceResponse consumerResponse = (ServiceResponse)session.getAttribute("ServiceResponse");
        Consumer consumer = (Consumer) consumerResponse.getdata();

        ServiceResponse serviceResponse = informationService.update(consumer,consumerName,consumerGender,consumerEmail,
                consumerHobby,Other1,mainImgUrl);


        ModelAndView mav = new ModelAndView();



        session.setAttribute("ServiceResponse",serviceResponse); // 绑定登录用户 到session中

        mav.setViewName("index");
        return mav;

    }

    @ResponseBody
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Map<String, Object> uploadFile(HttpServletRequest servletRequest, @RequestParam("file") MultipartFile file)
            throws IOException {
        // 如果文件内容不为空，则写入上传路径
        if (!file.isEmpty()) {
            //取得原文件名字
            String fileName = file.getOriginalFilename();
            //取得文件扩展名
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            //提取系统时间作为新文件名
            String prefix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date().getTime());
            //保存路径
            // 上传文件名
            String filename = prefix+suffix;
            System.out.println("11"+filename);
            String imgUrl=null;
            try {
                imgUrl = UploadImgUtil.upload(file.getBytes(),file.getOriginalFilename());
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(imgUrl);

            Map<String, Object> res = new HashMap<>();
            // 返回的是一个url对象,图片名称
            res.put("url", imgUrl);
            return res;
        } else {
            return null;
        }
    }

}
